package com.study.youlai.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.study.youlai.common.result.AddressResponse;
import com.study.youlai.common.util.HttpsUtil;
import com.study.youlai.system.service.IWeatherService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName: WeatherServiceImpl
 * @Description:
 * @Author: li
 * @Date: 2025/2/14 10:47
 * @Version 1.0
 **/
@Service
@RequiredArgsConstructor
public class WeatherServiceImpl implements IWeatherService {

    private final RestTemplate restTemplate;
    private String apiKey = "DtGypdF0L5nz7Bx99IqfUixPkmeaSOVf";


    @Override
    public String getWeather(String city) {
        String url = "https://api.map.baidu.com/telematics/v3/weather?location=" + city + "&output=json&ak=" + apiKey;
        return restTemplate.getForObject(url, String.class);
    }

    @Override
    public String getAddressByIp(String ip) {
        String url = "http://api.map.baidu.com/location/ip?ak=DtGypdF0L5nz7Bx99IqfUixPkmeaSOVf"  + "&ip=" + ip + "&coor=bd09ll";
        String response = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(response);

        if (0 == jsonObject.getInteger("status")) {
            JSONObject content = jsonObject.getJSONObject("content");

            AddressResponse data = JSONObject.parseObject(jsonObject.getString("content"), AddressResponse.class);

            String addressDetail = data.getAddress_detail();
            JSONObject json = JSONObject.parseObject(addressDetail);

            String adcode = (String)json.get("adcode");

            String address = content.getString("address");

            try {
                String apiUrl = "https://restapi.amap.com/v3/weather/weatherInfo?city="+adcode+"&key=cdfe8d09b3daa431e0cb64011b7fd1d9";
                String result = HttpsUtil.get(apiUrl);

                System.out.println(result);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            return address;
        }
            return null;

    }


    public String getWeatherByLatLng(double latitude, double longitude, String apiKey) throws Exception {
        String url = "https://devapi.qweather.com/v7/weather/now?location=" + longitude + "," + latitude + "&key=" + apiKey;
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(response);
        if ("200".equals(root.path("code").asText())) { // 请求成功
            JsonNode now = root.path("now");
            return "当前温度：" + now.path("temp").asText() + "℃, 天气状况：" + now.path("text").asText();
        } else {
            throw new RuntimeException("Failed to get weather info: " + root.path("message").asText());
        }
    }


}
